<template>
  <div id="app">
    <!-- 头部 -->
    <Topbar :key="TopbarKey"></Topbar>
    <!-- 导航 -->
    <Header></Header>
    <router-view @updataToBarkey="updataKeyFn" />
    <!-- 底部 -->
    <Footer></Footer>
    <!-- 登录模态框 -->
    <!-- 需要一个状态控制login是否显示 -->
    <Login v-show="isShowLoginModal"></Login>
    <transition name="slide">
      <Toast v-show="isShowToast" />
    </transition>
    <!-- <JfTitle /> -->
  </div>
</template>
<script>
import Topbar from "@/components/TopBar.vue";
import Header from "@/components/Header.vue";
import Footer from "@/components/Footer.vue";
import Login from "@/components/Login.vue";
import Toast from "@/components/Toast.vue";
// import JfTitle from "@/components/home/JfTitle.vue";
// import axios from "@/request/request";
// { JingpinAPI }按需导入，如果想要添加其他的就在后面加一个逗号再加名字
import { JingpinAPI, UserProfilesAPI } from "@/request/api";
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      TopbarKey: 0,
    };
  },
  components: {
    Header,
    Topbar,
    Footer,
    Login,
    Toast,
    // JfTitle,
  },
  computed: {
    ...mapState({
      isShowLoginModal: (state) => state.showModal.isShowLoginModal,
      isShowToast: (state) => state.ShowToast.isShowToast,
    }),
  },
  // methods: {
  //   ...mapActions({
  //     asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
  //   }),
  // },
  async created() {
    // 缺点
    // 1.服务器url地址都一样，每个组件调用时都需要复制过去
    // 一旦接口地址发生变化，所有调用了接口的组件都需要修改，
    // 2.每次请求时都需要手动编写config携带toten（多个组件携带代码重复）
    // 3.优化then()写法，async + await
    // JingpinAPI().then(
    //   (res) => {
    //     console.log("res2:", res);
    //   },
    //   (err) => {
    //     console.log(err);
    //   }
    // );

    let res = await JingpinAPI();
    // console.log("res3:", res);

    // 获取用户信息并更新vuex的数据
    // this.asyncChangeUserInfo();
  },
  watch: {
    "$route.path": {
      handler(newVal, oldVal) {
        // 单路由地址发生变化时，让topbar重载(重新加载)
        if (newVal != oldVal) {
          this.TopbarKey++;
        }
      },
    },
  },
  methods: {
    updataKeyFn() {
      this.TopbarKey++;
    },
  },
};

// 获取配置文件
console.log("配置地址:" + process.env.VUE_APP_BASE_URL);
console.log("配置名称:" + process.env.VUE_APP_MY_TEXT);
</script>


<style lang="less">
/* 入场的起始状态 = 离场的结束状态 */
.slide-enter,
.slide-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: opacity 0.3s linear;
}

.slide-enter-to,
.slide-leave {
  opacity: 1;
}
</style>
